package com.heaven.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heaven.mapper.ItemMapper;
import com.heaven.pojo.Item;
import com.heaven.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class ItemServiceImpl implements ItemService{
    @Autowired
    private ItemMapper itemMapper;

    @Override
    public PageResult getItemList(PageResult pageResult) {
        QueryWrapper<Item> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("title", pageResult.getQuery());

        IPage<Item> page = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        page = itemMapper.selectPage(page,queryWrapper);

        long total = page.getTotal();
        List<Item> list = page.getRecords();

        return pageResult.setTotal(total).setRows(list);
    }

    @Override
    public void updateItemStatus(Item item) {
        item.setUpdated(new Date());
        itemMapper.updateById(item);
    }

    @Override
    public Item getItemById(Integer id) {
        return itemMapper.getItemById(id);
    }

    @Override
    public void deleteItemById(Integer id) {
        itemMapper.deleteById(id);
    }

    @Override
    public void updateItem(Item item) {
        itemMapper.updateItem(item);

    }


}
